package control;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.Util;
import dao.UserData;

/**
 * Servlet implementation class ChangePassword
 */
public class ChangePassword extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChangePassword() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String email = request.getParameter("email");
		String pass = request.getParameter("pass");
		String passChange1 = request.getParameter("passChange1");
		String passChange2 = request.getParameter("passChange2");
		String email_err = "";
		String user_err = "";
		String pass_err = "";
		String passChange1_err ="";
		String passChange2_err="";
		UserData ud = new UserData();
		boolean checkEmail = ud.checkEmail(email);
		boolean checkpass = ud.checkLogin(email, pass);
		if (pass.equals("")) {
			pass_err = "Bạn chưa nhập mật khẩu !";
		} else if (!checkpass) {
			pass_err = "Bạn nhập mật khẩu không đúng";
		}
		
		if (passChange1.equals("")) {
			passChange1_err = "Bạn chưa nhập mật khậu mới !";
		} 
		
		if (passChange2.equals("")) {
			passChange2_err = "Bạn chưa nhập xác nhận mật khậu mới!";
		} 
		if(!passChange1.equals(passChange2)){
			passChange1_err = "Mật khẩu nhập lại không đúng";
		}
		
		
		email = email.trim();
		if (email.equals("")) {
			email_err += "bạn chưa nhập email";
		} else {
			Pattern patternObject = Pattern
					.compile("^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
			Matcher matcherObject = patternObject.matcher(email);
			if (!matcherObject.matches()) {
				email_err += "bạn nhập email không hợp lệ";
			} else {
				if (!checkEmail) {
					email_err += "Bạn Nhập email không đúng";
				}
			}

		}

		String url = "/confirm.jsp";
		request.setAttribute("email", email);
		if (checkEmail && checkpass && passChange1.equals(passChange2)) {
			ud.updatePass(passChange1, email);

		}
		if (email_err.length() > 0 || user_err.length() > 0 ||passChange2_err.length() > 0 || passChange1_err.length()>0|| pass_err.length()>0) {
			url = "/changePassword.jsp";
			request.setAttribute("email_err", email_err);
			request.setAttribute("pass_err", pass_err);
			request.setAttribute("passChange1_err", passChange1_err);
			request.setAttribute("passChange2_err", passChange2_err);

		}
		RequestDispatcher rd = getServletContext().getRequestDispatcher(url);
		rd.forward(request, response);
		
	}

}
